# -*- coding: utf-8 -*-
import mysqlDB
class Opencart:
    def __init__(self):
        """
        :rtype : object
        """
        self.odb =mysqlDB.Mydb(host='localhost', dataBase='ssdinor_com', user='root', passwd='123456')
        self.db =mysqlDB.Mydb(host='localhost', dataBase='r_com', user='root', passwd='123456')
    #------------------------------------------ End def __init__()

    def update(self):
        self.odb.dbLoad()
        self.odb.dbLoad()
        """
        INSERT INTO `oc_product` (`product_id`, `model`, `quantity`, `stock_status_id`, `image`, `manufacturer_id`, `shipping`, `price`, `points`, `tax_class_id`, `date_available`, `weight`, `weight_class_id`, `length`, `width`, `height`, `length_class_id`, `subtract`, `minimum`, `sort_order`, `status`, `viewed`, `date_added`, `date_modified`) VALUES
(77, 'DBDR43098', 1, 6, 'img', 0, 1, '2.2500', 0, 0, '2015-03-15', '0.00000000', 1, '0.00000000', '0.00000000', '0.00000000', 1, 1, 1, 1, 1, 0, '2015-03-15 22:18:35', '0000-00-00 00:00:00');
        """
        query = self.odb.query(u"SELECT `id`,`title`,`thumb`,`price`,`xinghao`,`cailiao`,`mprice`,`size`,`weight`,`content`,`keywords`,`catid` FROM `iic_product` order by id DESC;")
        sale=0
        for row in query:
            sql="SELECT p.code,p.image,p.ygstatus,p.name,p.mian_cat cate_id,p.price jiayi,ppo.price,ppo.add,ppo.percent FROM `product_price_o2o` as ppo LEFT JOIN product as p on ppo.product_id = p.id WHERE ( ppo.user_id = '%s' ) AND ( ppo.product_id = '%s' ) LIMIT 1;" % (row['user_id'],row['product_id'])
            info=self.odb.query(sql)
            res=info[0]
            print("%s   " % row['id'])
            sale=0
            if res['price']>0:
                sale=res['price']
            elif res ['add'] > 0:
                sale = res ['jiayi'] + res ['add']
            elif res ['percent'] > 0:
                sale =res ['jiayi']* (1 +(res ['percent'] * 0.01))
            sale=round(sale,0)
            #print(sale)
            instesql="UPDATE `product_des_o2o` SET `jiayi`=%s,`sale`='%s',`status`=%s,`ygstatus`=%s,`cate_id`=%s WHERE ( `product_id` = %s ) AND ( `user_id` = %s )" % (res ['jiayi'],sale,row['status'],res['ygstatus'],res['cate_id'],row['product_id'],row['user_id'])
            #print(instesql)
            self.odb.save(instesql)
        self.odb.dbClose()
    #------------------------------------------ End def assets()
#-------------------------------------------------- End class UserAssets

if __name__ == '__main__':
    desc=Opencart()
    desc.update()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
